package com.jvxb.demo.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMqConfig {

    public static final String ORDER_TOPICEXCHANGE = "order_topic_exchange";
    public static final String SMS_QUEUE = "sms.topic.queue";
    public static final String MAIL_QUEUE = "mail.topic.queue";
    //只有消息的路由键等于sms.topic.binding，才能路由到邮箱(mail)队列
    public static final String MAIL_ORDER_BINDING_KEY = "mail.topic.binding";
    //只要消息的路由键以 topic.binding结尾，则路由到短信(sms)队列
    public static final String SMS_ORDER_BINDING_KEY = "#.topic.binding";

    //声明订单主题交换机
    @Bean
    TopicExchange orderTopicExchange() {
        return new TopicExchange(ORDER_TOPICEXCHANGE);
    }

    //短信队列
    @Bean
    public Queue smsTopicQueue() {
        return new Queue(SMS_QUEUE);
    }

    //邮件队列
    @Bean
    public Queue mailTopicQueue() {
        return new Queue(MAIL_QUEUE, true);  //true 是否持久
    }

    //订单交换机与短信队列绑定
    @Bean
    Binding smsBinding() {
        return BindingBuilder.bind(smsTopicQueue()).to(orderTopicExchange()).with(SMS_ORDER_BINDING_KEY);
    }

    //订单交换机与邮件队列绑定
    @Bean
    Binding mailBinding() {
        return BindingBuilder.bind(mailTopicQueue()).to(orderTopicExchange()).with(MAIL_ORDER_BINDING_KEY);
    }
}